﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace TSSControlLibrary 
{
    public partial class DisPlayIOInfoUserControl : UserControl
    {
        /// <summary>
        /// 
        /// </summary>
        private int oldLenth; 
        /// <summary>
        /// 
        /// </summary>
        private int newLength;
        /// <summary>
        /// 
        /// </summary>
        private bool isPressEnterKey;
        /// <summary>
        /// 显示输出信息的控件的构造函数、初始化控件的一些私有成员
        /// </summary>
        public DisPlayIOInfoUserControl()
        {
            InitializeComponent();         
            oldLenth = 0;
            newLength = 0;     
            isPressEnterKey = false;
        }
        /// <summary>
        /// 重置控件的文本框内容的长度
        /// </summary>
        /// <param name="length"></param>
        public void ResetoldLenth(int length)
        {
            this.oldLenth = length;
        }
        /// <summary>
        /// 添加一行输出信息
        /// </summary>
        /// <param name="oneText"></param>
        public void WriteOneLine(string oneText)
        {
            Action action = delegate
            {
                this.richTextBox1.AppendText(oneText + "\n");
            };
            richTextBox1.Invoke(action);
            oldLenth = oldLenth + oneText.Length+1;
        }
        /// <summary>
        /// 设置控制台前景字体颜色
        /// </summary>
        /// <param name="oneViewLst"></param>
        private void ForegroundColor(ListViewItem oneViewLst)
        {
            if (Console.ForegroundColor == ConsoleColor.Gray)
            {
                oneViewLst.ForeColor = Color.Gray;
            }
        }
        /// <summary>
        /// 读入一行信息
        /// </summary>
        /// <returns></returns>
        public string ReadOneLine()
        {
            if (this.isPressEnterKey)
            {
                isPressEnterKey = false;
                string rs = this.richTextBox1.Text.Substring(oldLenth, newLength - oldLenth).Replace("\n", "");
                oldLenth = newLength;
                return rs;
            }
            //Application.DoEvents();
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 从键盘读入一个字符事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void richtextbox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.isPressEnterKey = true;
                this.newLength = this.richTextBox1.Text.Length;
            }
            else
            {
               
            }
        }
        /// <summary>
        /// 控制台内容发生变化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            this.richTextBox1.Focus();
            this.richTextBox1.Select(this.richTextBox1.Text.Length, 0);
            this.richTextBox1.ScrollToCaret();                    
        }
    }
}
